<?php

declare(strict_types=1);

namespace ImiApp\ApiServer\Common\Auth;

// use Imi\Server\Http\Route\Annotation\Action;
// use Imi\Server\Http\Route\Annotation\Controller;
// use Imi\Server\Http\Route\Annotation\Route;
use ImiApp\ApiServer\Api\Model\FaUser as User;
use Psr\Http\Message\ResponseInterface;
use Imi\Aop\Annotation\Inject;
// use Phpben\Imi\Validate\Annotation\Validate;
use Imi\Server\Http\Message\Proxy\RequestProxy;




use app\common\model\UserRule;
use ImiApp\ApiServer\Common\AuthApi;
use ImiApp\ApiServer\Common\Auth2;
use Imi\Config;
use Imi\Db\Db;


use Imi\Bean\Annotation\Base;
use Imi\Bean\Annotation\Parser;

use ImiApp\ApiServer\Common\Token;

class AuthManager
{
    protected $config;
    protected $name;

    public function __construct(?string $name = null)
    {
        var_dump("AuthManager__construct", $name);
        $this->name = $name ?? 'default';
        $this->config = Config::get('@app.beans.AuthConfig');
    }

    /**
     * @throws Exception
     */
    public function __call($name, $arguments)
    {
        $guard = $this->guard();

        // $request = new RequestProxy;

        // $token =$request->getHeaderLine('token');
        // $guard->init($token);
        var_dump("AuthManager __call  this->guard", $name, $arguments);
        // var_dump('this->guard',$guard);
        if (method_exists($guard, $name)) {
            return call_user_func_array([$guard, $name], $arguments);
        }
        throw new Exception('AuthApi(' . $name . ') not defined.');
    }

    /**
     * @throws Exception
     */
    public function guard(?string $name = null)
    {

        return AuthApi::instance();
        // $name = $name ?: $this->name;
        // var_dump('name_guard',$name);
        // if (!isset($this->config[$name])) {
        //     throw new Exception('AuthConfig(' . $name . ') not defined.');
        // }
        // $config = $this->config[$name];
        // return new $config['auth']($config);
    }

    /**
     * 返回Auth实例
     * @param int|null $user_id 用户ID
     * @param string|null $name
     * @return AuthApi
     * @throws Exception
     */
    public function auth(?int $user_id = null, ?string $name = null): AuthApi
    {
        // $name = $name ?: $this->name;
        // if (!isset($this->config[$name])) {
        //     throw new Exception('AuthConfig(' . $name . ') not defined.');
        // }
        // $config = $this->config[$name];
        // return AuthApi::instance($config)->setUserId($user_id ?? $this->guard()->id);
        return AuthApi::instance();
    }

    /**
     * @throws Exception
     */
    public function init2(?string $token = null)
    {
        $data = Token::get($token);
        if (!$data) {
            return false;
        }
        var_dump('AuthManager init2 data', $data);
        $user_id = intval($data['user_id']);


        if ($user_id > 0) {
            $user = User::find($user_id);

            var_dump('AuthManager init-user_id', $user->id);
            if (!$user) {
                // $this->setError('Account not exist');
                throw new Exception('Account not exist');
                return false;
            }
            if ($user['status'] != 'normal') {
                // $this->setError('Account is locked');
                throw new Exception('Account is locked');
                return false;
            }
            $this->init($token);
            return true;
        } else {
            // $this->setError('You are not logged in');
            return false;
        }
        return true;
    }
}
